/*
 * @Author: CQJ
 * @Date: 2022-08-25 20:00:27
 * @LastEditors: CQJ
 * @LastEditTime: 2022-09-23 15:02:24
 * @Description:
 */
import { storage } from '@/utils/storage'
import { reactive } from 'vue'

type Key = string
interface ManagerState {
  formVisible: boolean
  resetPwdVisible: boolean
  selectedRowKeys: Key[]
  isAdd: boolean
}

// 管理员列表状态管理
export const managerState = reactive<ManagerState>({
  formVisible: false,
  resetPwdVisible: false,
  selectedRowKeys: [],
  isAdd: false
})
//6-20个字符，可包含英文字母、数字和标点符号（除空格）
// const pwdReg = /^(?![\d]+$)(?!.*[\s])(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}|[\dA-Za-z_!@#$%^&*`~()-+=.]{6,20}$/
const pwdReg = /^[\dA-Za-z_!@#$%^&*`~()-+=.]{6,20}$/
const pwdReg1 = /^[da-zA-Z,.?/\\{~!@#$%^&*()_+|}{\]["":;?><=}]$/
//4-18个字符，可包含英文字母、数字、下划线、@
const nameReg = /^[0-9a-zA-Z_]{4,18}$/
//添加管理用户名规则
const validateUserName = (rule: any, value: any, callback: any) => {
  if (!value.trim()) {
    return Promise.reject('请输入用户名')
  } else if (!nameReg.test(value)) {
    return Promise.reject('请输入4-18个字符，可包含英文字母、数字、下划线')
  } else {
    return Promise.resolve()
  }
}
export const addAccountRules = reactive({
  user_name: [{ required: true, whitespace: true, validator: validateUserName }]
})

//管理员列表-重置密码规则
const validateResetPwd = (rule: any, value: any, callback: any) => {
  if (!value) {
    return Promise.reject('请输入密码')
  } else if (!pwdReg.test(value)) {
    return Promise.reject('请输入6-20个字符，可包含英文字母、数字和标点符号（除空格）')
  } else {
    return Promise.resolve()
  }
}
export const resetPwdRules = reactive({
  password: [{ required: true, validator: validateResetPwd, trigger: 'blur' }]
})
interface IPwdFormState {
  admin_id: string
  old_password: string
  new_password: string
  affirm_password: string
}
//个人中心-修改密码密码规则
export const pwdFormState = reactive<IPwdFormState>({
  admin_id: '',
  old_password: '',
  new_password: '',
  affirm_password: ''
})
export const validatePassword = (rule: any, value: any, callback: any) => {
  if (!value) {
    return Promise.reject('请输入原密码')
  } else if (!pwdReg.test(value)) {
    return Promise.reject('请输入6-20个字符，可包含英文字母、数字和标点符号（除空格）')
  } else {
    return Promise.resolve()
  }
}
export const validateNewPassword = (rule: any, value: any, callback: any) => {
  if (!value) {
    return Promise.reject('请输入新密码')
  } else if (pwdFormState.old_password === value) {
    return Promise.reject('新密码不能与旧密码相同')
  } else if (!pwdReg.test(value)) {
    return Promise.reject('请输入6-20个字符，可包含英文字母、数字和标点符号（除空格）')
  } else {
    return Promise.resolve()
  }
}
export const validateConfirmPassword = (rule: any, value: any, callback: any) => {
  if (!value) {
    return Promise.reject('请输入确认密码')
  } else if (value !== pwdFormState.new_password) {
    return Promise.reject('确认新密码不一致')
  } else {
    return Promise.resolve()
  }
}
export const pwdFormRules = reactive({
  old_password: [{ required: true, validator: validatePassword, trigger: 'blur' }],
  new_password: [{ required: true, validator: validateNewPassword, trigger: 'blur' }],
  affirm_password: [{ required: true, validator: validateConfirmPassword, trigger: 'blur' }]
})
